package com.kgcx.price.controller;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.dbutils.core.model.RowMap;
import com.kgcx.price.domain.MileageRecord;
import com.kgcx.price.service.MileageRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "司机里程上传相关接口", tags = "司机里程上传相关接口")
@RestController
@RequestMapping("mileagerecord")
public class MileageRecordController extends BaseController {
    @Autowired
    MileageRecordService mileageRecordService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "司机开始里程上传接口", httpMethod = "POST", notes = "这是用来上传司机开始里程的接口")
    @PostMapping("recordStartMile")
    public ReturnData<MileageRecord> recordStartMile(@RequestBody MileageRecord mileageRecord) {
        return mileageRecordService.recordStartMile(mileageRecord);
    }

    @ApiOperation(value = "司机结束里程上传接口", httpMethod = "POST", notes = "这是用来上传司机结束里程的接口")
    @PostMapping("recordEndMile")
    public ReturnData<MileageRecord> recordEndMile(@RequestBody MileageRecord mileageRecord) {
        return mileageRecordService.recordEndMile(mileageRecord);
    }

    @ApiOperation(value = "日志测试接口", httpMethod = "GET", notes = "日志测试接口")
//    @ApiImplicitParams({@ApiImplicitParam(name = "dirverId", value = "城市Id", paramType = "query", dataType = "String")})
    @GetMapping("test")
    public ReturnData test(String dirverId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dirverId", null);
        jsonObject.put("city", null);
        jsonObject.put("car", null);

        return new ReturnData<>(jsonObject);

//        System.out.println(stringRedisTemplate.opsForList().rightPush("mileageRecord_"+ dirverId, jsonObject.toJSONString()));
    }

    @ApiOperation(value = "根据司机Id取得实时坐标接口", httpMethod = "GET", notes = "这是根据司机Id取得实时坐标接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "driverIds", value = "司机Id", paramType = "query", dataType = "String")})
    @GetMapping("getDriverLocation")
    public ReturnData<Map<String, RowMap>> getDriverLocation(@RequestParam("driverIds") String driverIds) {
        List<String> list = HelpUtil.splitToList(driverIds, String.class);
        Map<String, RowMap> map = new HashMap<>();

        for (String driverId : list) {
            String value = stringRedisTemplate.opsForValue().get("driver_" + driverId);

            if (!HelpUtil.isEmpty(value)) {
                JSONObject jsonObject = JSONObject.parseObject(value);
                map.put(driverId, new RowMap("lng", jsonObject.getString("lng")).put("lat", jsonObject.getString("lat")));
            }
        }

        return new ReturnData<>(map);
    }
}
